package com.sinopec.basemodule.widget.uploadview

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.jakewharton.rxbinding3.view.clicks
import com.sinopec.basemodule.R
import com.sinopec.basemodule.model.ImageBean
import com.sinopec.basemodule.utils.kotlin.glideIntoAsBitmapPath
import com.sinopec.basemodule.utils.kotlin.setNewVisibility
import com.sinopec.basemodule.view.PhotoPreview
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import java.util.*
import java.util.concurrent.TimeUnit

/**
 * 类描述：上传view  <br/>
 * 创建人：lwj<br/>
 * 创建时间：2020/4/13 19:27 <br/>
 */
class UploadView @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
        RelativeLayout(context, attrs, defStyleAttr) {
    companion object {
        //无图
        const val MODEL_NO_PIC = 0

        //重新上传有图有蒙版
        const val MODEL_RE_UPLOAD = 1

        //有图有水印
        const val MODEL_WATERMARK = 2

        //光图片
        const val MODEL_PIC = 3
    }

    interface Callback {
        fun uploadPic()
    }

    private var mCallback: Callback? = null

    private val mAllView by lazy {
        findViewById<RelativeLayout>(R.id.all_view)
    }
    private val mUploadViewRl by lazy {
        findViewById<RelativeLayout>(R.id.upload_view_rl)
    }
    private val mUploadViewIv by lazy {
        findViewById<ImageView>(R.id.upload_view_iv)
    }
    private val mUploadViewV by lazy {
        findViewById<View>(R.id.upload_view_v)
    }
    private val mUploadViewTv by lazy {
        findViewById<TextView>(R.id.upload_view_tv)
    }
    private val mUploadViewWatermarkTv by lazy {
        findViewById<TextView>(R.id.upload_view_watermark_tv)
    }
    private val mUploadViewStrLl by lazy {
        findViewById<LinearLayout>(R.id.upload_view_str_ll)
    }
    private val mUploadViewStrTv by lazy {
        findViewById<ImageView>(R.id.upload_business_str_tv)
    }

    //无图时上传文字
    private var mUploadViewStrTvStr: String? = null
    private var mModel: Int = MODEL_NO_PIC
    private var mNetImagePath: String? = null

    private val mDisposable by lazy {
        CompositeDisposable()
    }

    init {
        View.inflate(context, R.layout.bm_upload_view, this)

        val array = context?.obtainStyledAttributes(attrs, R.styleable.UploadView)
        array?.let {
            mUploadViewStrTvStr = it.getString(R.styleable.UploadView_uploadview_text)
            mModel = it.getInt(R.styleable.UploadView_uploadview_model, MODEL_NO_PIC)
        }
        array?.recycle()

        changeModel(mModel)
        initEvent()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mDisposable.clear()
    }

    //初始化点击事件
    private fun initEvent() {
        mDisposable.add(mAllView.clicks()
                .throttleFirst(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    when (mModel) {
                        MODEL_NO_PIC -> {
                            //无图
                            mCallback?.uploadPic()
                        }
                        MODEL_RE_UPLOAD -> {
                            //重新上传有图有蒙版
                            mCallback?.uploadPic()
                        }
                        MODEL_WATERMARK -> {
                            //有图有水印
                            showImage(mNetImagePath)
                        }
                        MODEL_PIC -> {
                            //光图片
                            showImage(mNetImagePath)
                        }
                    }
                })
    }

    fun setCallback(callback: Callback) {
        mCallback = callback
    }

    /**
     * @param model MODEL_NO_PIC:无图
     *              MODEL_RE_UPLOAD:重新上传有图有蒙版
     *              MODEL_WATERMARK:有图有水印
     *              MODEL_PIC:光图片
     *
     * @param netImagePath 网络图片路径
     */
    @JvmOverloads
    fun changeModel(model: Int, netImagePath: String? = null) {
        mNetImagePath = netImagePath
        mModel = model
        when (model) {
            MODEL_NO_PIC -> {
                //无图
                mUploadViewRl.setNewVisibility(View.GONE)
                mUploadViewStrLl.setNewVisibility(View.VISIBLE)
            }
            MODEL_RE_UPLOAD -> {
                //重新上传有图有蒙版
                mUploadViewStrLl.setNewVisibility(View.GONE)
                mUploadViewRl.setNewVisibility(View.VISIBLE)
                mUploadViewV.setNewVisibility(View.VISIBLE)
                mUploadViewTv.setNewVisibility(View.VISIBLE)
                mUploadViewWatermarkTv.setNewVisibility(View.GONE)
                mUploadViewIv.glideIntoAsBitmapPath(mNetImagePath)
            }
            MODEL_WATERMARK -> {
                //有图有水印
                mUploadViewStrLl.setNewVisibility(View.GONE)
                mUploadViewRl.setNewVisibility(View.VISIBLE)
                mUploadViewV.setNewVisibility(View.GONE)
                mUploadViewTv.setNewVisibility(View.GONE)
                mUploadViewWatermarkTv.setNewVisibility(View.VISIBLE)
                mUploadViewIv.glideIntoAsBitmapPath(mNetImagePath)
            }
            MODEL_PIC -> {
                //光图片
                mUploadViewStrLl.setNewVisibility(View.GONE)
                mUploadViewRl.setNewVisibility(View.VISIBLE)
                mUploadViewV.setNewVisibility(View.GONE)
                mUploadViewTv.setNewVisibility(View.GONE)
                mUploadViewWatermarkTv.setNewVisibility(View.GONE)
                mUploadViewIv.glideIntoAsBitmapPath(mNetImagePath)
            }
        }
    }

    /**
     * 显示预览图片，暂时只有网络图片
     */
    private fun showImage(netImagePath: String? = null) {
        if (context == null) {
            return
        }
        val list = ArrayList<ImageBean>()
        val imageBean = ImageBean("")
        imageBean.netPath = netImagePath
        list.add(imageBean)
        val b = PhotoPreview.builder()
        if (mModel == MODEL_WATERMARK) {
            b.setWaterMarkLayoutRes(R.layout.minemodule_watermark_hint)
        }
        b.setPhotos(list)
                .setCurrentItem(0)
                .start(context as Activity)
    }

}